// pages/MySnapshots/Mysnapshots.js
const db = wx.cloud.database();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vanTop: 0,
    windowHeight: 0,
    windowWidth: 0,
    swiperViewHeight: 0,
    currentIndex: 0,
    navList: [{
        id: 1,
        title: '待审核'
      },
      {
        id: 2,
        title: '审核通过'
      },
      {
        id: 3,
        title: '已退回'
      }
    ],
    readyapproveList: [],
    approvedList: [],
    unapprovedList: [],
    loading: false
  },
  onPullDownRefresh() {
    this.setData({
      loading:true,
      readyapproveList: [],
      approvedList: [],
      unapprovedList: [],
    })
    this._index=0
    this.getAllData();
    wx.stopPullDownRefresh()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this._index=0
    this.setData({
      vanTop: app.globalData.vanTop
    })
    this.getAllData()
  },
  //到底加载更多数据
  onReachBottom(){
    this.getAllData()
  },
  getAllData() {
    this.setData({
      loading: true
    })
    //待审核
    let state0=db.collection("snapShot").orderBy('time', 'desc').skip(this._index*4).limit(4).where({
      state: 0,
      _openid: app.globalData.openid
    }).get().then((res) => {
      if (res.data.length !== 0) {
        this.setData({
          readyapproveList: this.data.readyapproveList.concat(res.data)
        })
      }
    })
    //审核通过
    let state1=db.collection("snapShot").orderBy('time', 'desc').skip(this._index*6).limit(6).where({
      state: 1,
      _openid: app.globalData.openid
    }).get().then((res) => {
      if (res.data.length !== 0) {
        this.setData({
          approvedList: this.data.approvedList.concat(res.data)
        })
      }
    })
    //已退回
    let stateout=db.collection("snapShot").orderBy('time', 'desc').skip(this._index*6).limit(6).where({
      state: -1,
      _openid: app.globalData.openid
    }).get().then((res) => {
      if (res.data.length !== 0) {
        this.setData({
          unapprovedList: this.data.unapprovedList.concat(res.data)
        })
      }
    })
    Promise.all([state0,state1,stateout]).then(()=>{
      this._index++
      this.setData({
        loading:false
      })
    })
    
  },
  modify(e) {
    const item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: `../modifySnapshots/modifySnapshots?_id=${item._id}&title=${'snapShot'}`,
    })
  },
  delete(e){
    const id=e.currentTarget.dataset.doc
    wx.showModal({
      title: '删除确认',
      content: '删除该条数据后将无法恢复',
      success: (res)=>{
        if (res.confirm) {
          db.collection("snapShot").doc(id).remove({
            success:()=>{
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000,
              })
              wx.startPullDownRefresh()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
    //跳转详情
    click(e){
      const item = e.currentTarget.dataset.item
      wx.navigateTo({
        url: `../../../pages/snapShotDetail/snapShotDetail?_id=${item._id}&title=${'snapShot'}`,
      })
    },
})